<?php
namespace app\common\controller;
use app\admin\model\AdminGroup;
use app\data\cache\CacheToken;
use app\BaseController;
use ba\Auth;
use think\App;
use think\facade\Db;
use think\model;
use  think\facade\Request;
use \Firebase\JWT\JWT;

class PCBase extends BaseController
{
    public $api_result;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->api_result = new ApiResult();
        $this->checkAccessToken();
    }

    public function jsonResponse($code = 200, $success = false, $msg = "加载错误", $data = [])
    {
        $obj = new \stdClass();
        $obj->code = $code;
        $obj->success = $success;
        $obj->msg = $msg;
        $obj->data = $data;
        if ($obj) {
            return json($obj);
        } else {
            abort(404);
        }

    }


    public function jsonMessage($code = 200, $success = false, $msg = "加载错误", $data = null)
    {

        $this->api_result->code = $code;
        $this->api_result->success = $success;
        $this->api_result->msg = $msg;
        $this->api_result->data = null;
        if ($data) $this->api_result->data = $data;
        if ($this->api_result) {
            return json($this->api_result);
        } else {
            abort(404);
        }
    }


    public function checkAccessToken()
    {

        $accessToken = Request::header('token');
        if (empty($accessToken)) {
            die(json_encode(['code' => 601, 'success' => false, 'msg' => '未授权登录']));
        }
        $decodeinfo = $this->getInfoByToken($accessToken);
        $admin_info = $this->object_to_array($decodeinfo);
        $info = CacheToken::getAdminPcData($admin_info['tel']);
        if ($info != $admin_info['token']) {
            die(json_encode(['code' => 601, 'success' => false, 'msg' => '鉴权无效']));
        }
        return true;
    }


    //获取当前操作员工的tel;
    public function getAdminTel()
    {
        $accessToken = Request::instance()->header('token');
        $decodeinfo = $this->getInfoByToken($accessToken);
        //获取员工的tel;
        $admin_info = $this->object_to_array($decodeinfo);
        return $admin_info['tel'];
    }


    //获取当前操作员工的分组ID;
    public function getAdminGroupId()
    {
        $accessToken = Request::instance()->header('token');
        $decodeinfo = $this->getInfoByToken($accessToken);
        //获取员工的id;
        $admin_info = $this->object_to_array($decodeinfo);
        return $admin_info['groupId'];
    }


    //获取当前操作员工的权限ID;
    public function getAdminAuthId()
    {
        $accessToken = Request::instance()->header('token');
        $decodeinfo = $this->getInfoByToken($accessToken);
        //获取员工的id;
        $admin_info = $this->object_to_array($decodeinfo);
        return $admin_info['admin_group_id'];
    }




    //获取当前操作员工的ID;
    public function getAdminId()
    {
        $accessToken = Request::instance()->header('token');
        $decodeinfo = $this->getInfoByToken($accessToken);
        //获取员工的id;
        $admin_info = $this->object_to_array($decodeinfo);
        return  $admin_info['id'];
    }



    //获取当前员工信息
    public function getAdminInfo()
    {
        $accessToken = Request::instance()->header('token');
        $decodeinfo = $this->getInfoByToken($accessToken);
        return $this->object_to_array($decodeinfo);
    }

    private function getInfoByToken($token)
    {
        try {
            $decoded = JWT::decode($token, env('JWT_JWT_SALT'), array('HS256'));
            return $decoded;
        } catch (\Exception  $e) {
            die(json_encode(['code' => 601, 'success' => false, 'msg' => '鉴权无效']));
        }
    }

    /**
     * 对象 转 数组
     *
     * @param object $obj 对象
     * @return array
     */
    private function object_to_array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)object_to_array($v);
            }
        }
        return $obj;
    }


    /**
     * 排除入库字段
     * @param array $params
     * @return array
     */
    protected function excludeFields(array $params): array
    {
        if (!is_array($this->preExcludeFields)) {
            $this->preExcludeFields = explode(',', (string)$this->preExcludeFields);
        }

        foreach ($this->preExcludeFields as $field) {
            if (array_key_exists($field, $params)) {
                unset($params[$field]);
            }
        }
        return $params;
    }



}

